//
//  TrackDayDetailViewController.m
//  TheRoadAD
//
//  Created by 宁宁 on 16/8/18.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "TrackDayDetailViewController.h"
#import "TrackDayTableViewCell.h"//活动详情cell
#import "EventRegistrationTableViewCell.h"//报名cell
#import "TunerDetailCommentTableViewCell.h"//评论cell
#import "OthersHomePageViewController.h"//他人主页Controller
#import "ListScreenViewController.h"//列表筛选Controller
#import "WNY_Top_TableViewCell.h"
#import "WNY_TOP_Model.h"
#import "CustomCardView.h"
#import "CCDraggableContainer.h"
#import "InChargeAlertView.h"

@interface TrackDayDetailViewController () <UITableViewDelegate, UITableViewDataSource,CCDraggableContainerDataSource,
CCDraggableContainerDelegate>
@property (nonatomic,strong)UIButton *likeBtn;
@property (nonatomic,strong)UIButton *disLike;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CCDraggableContainer *container;
@property (nonatomic, strong) NSMutableArray *dataSources;
@property (nonatomic,strong)UIButton *backBtn;


@end

@implementation TrackDayDetailViewController
{
    WNY_TOP_Model *_model;
    
    NSMutableArray *_commentArray;
    NSMutableArray *_cellHeightArray;
    InChargeAlertView *alerView;
}


 static NSString *const TrackDayTableViewCellID = @"TrackDayTableViewCell";
 static NSString *const EventRegistrationTableViewCellID = @"EventRegistrationTableViewCell";
 static NSString *const TunerDetailCommentTableViewCellID = @"TunerDetailCommentTableViewCell";
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    [self createData];
    
    [self registerTableView];
    self.backBtn = [UIButton buttonWithType:0];
    [self.backBtn setBackgroundColor:[UIColor colorWithRed:1/255.0 green:1/255.0 blue:1/255.0 alpha:0.5] ];
//    self.backBtn.backgroundColor = [UIColor redColor];
    self.backBtn.hidden = YES;
    self.backBtn.frame = CGRectMake(0, -64, Screen_Width, Screen_Height + 64);
    [self.backBtn addTarget:self action:@selector(btnAction)forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow addSubview:self.backBtn];
}
- (void)btnAction{
    self.backBtn.hidden = YES;
    [self.container removeFromSuperview];
    [_likeBtn removeFromSuperview];
    [_disLike removeFromSuperview];
}
- (void)loadData {
    
    _dataSources = [NSMutableArray array];
    
    for (int i = 0; i < 9; i++) {
        NSDictionary *dict = @{@"image" : [NSString stringWithFormat:@"pic%d.jpg",i ],
                               @"title" : [NSString stringWithFormat:@"Page %d",i ]};
        [_dataSources addObject:dict];
    }
    
}
- (void)createData{
    
    NSArray *iconImageNamesArray = @[@"icon0.jpg",
                                     @"icon1.jpg",
                                     @"icon2.jpg",
                                     @"icon3.jpg",
                                     @"icon4.jpg",
                                     @"icon0.jpg",
                                     @"icon1.jpg",
                                     @"icon2.jpg",
                                     @"icon3.jpg"
                                     ];
    NSArray *arr = @[
                     @[@"Lister · 傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲"],
                     @[@"Lister · 傅傅", @"2025-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"],
                     @[@"Lister · 傅傅傅", @"2025-05-20 26:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"],
                     @[@"Lister · 傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"],
                     @[@"Lister · 傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"],
                     @[@"Lister · 傅傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲"],
                     @[@"Lister · 傅傅傅傅傅傅傅傅", @"2025-05-20 26:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲"],
                     @[@"ListerLister · 傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"],
                     @[@"ListerListerLister · 傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲"],
                     @[@"ListerListerListerLister · 傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲"],
                     @[@"ListerListerListerListerLister · 傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"]
                     ];

    
    _model = [[WNY_TOP_Model alloc] init];
    _model.title = @"标题题题题题题题题";
    _model.conxxxx =arr[arc4random_uniform(9)][0];
    _model.iconImagePath = iconImageNamesArray[arc4random_uniform(9)];
    _model.time = @"2016.8.11  10:30";
    _model.userName = @"用户名用户名用户名用户名用户名用户名";
    _model.imageLike = iconImageNamesArray[arc4random_uniform(9)];
    _model.numberOfDay = @"1";
    _model.overTime = @"2016.8.11  10:30";
    _model.numberOfPeople = @"2";
    _model.Drive = @"是";
    _model.location = @"秋名山";
    // 模拟“有或者无图片”
    int random = arc4random_uniform(1);
    if (random <= 8) {
        NSMutableArray *temp = [NSMutableArray new];
        
        int randomImagesCount = arc4random_uniform(9);
        for (int i = 0; i < randomImagesCount; i++) {
            int randomIndex = arc4random_uniform(9);
            NSString *text = iconImageNamesArray[randomIndex];
            [temp addObject:text];
        }
        NSMutableArray *aa = [NSMutableArray array];
        for (int i = 0; i < randomImagesCount; i++) {
            int randomIndex = arc4random_uniform(9);
            NSString *text = iconImageNamesArray[randomIndex];
            [aa addObject:text];
        }
        
        _model.imagePathsArray = [temp copy];
        _model.imageLike = [aa copy];
    }

    
    
    
    _commentArray = [NSMutableArray array];
       [_commentArray addObjectsFromArray:arr];
    
    
    if (!_cellHeightArray) {
        _cellHeightArray = [NSMutableArray array];
    }
    
    for (int i = 0; i < _commentArray.count; i++) {
        [_cellHeightArray addObject:@"0"];
    }


}

- (void)registerTableView{
    [self.tableView registerNib:[UINib nibWithNibName:@"WNY_Top_TableViewCell" bundle:nil] forCellReuseIdentifier:@"WNY_Top_TableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EventRegistrationTableViewCell" bundle:nil] forCellReuseIdentifier:EventRegistrationTableViewCellID];
    [self.tableView registerClass:[TunerDetailCommentTableViewCell class] forCellReuseIdentifier:TunerDetailCommentTableViewCellID];
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 2) {
        return _commentArray.count;
    }else return 1;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        WNY_TOP_Model  *demoVC14Modelodel = _model;
        return [self.tableView cellHeightForIndexPath:indexPath model:demoVC14Modelodel keyPath:@"model" cellClass:[WNY_Top_TableViewCell class] contentViewWidth:[self cellContentViewWith]];
        
        
    }else if (indexPath.section == 1){
        return 30;
    }else return [_cellHeightArray[indexPath.row] floatValue];
}
//section数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
//Reusable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        WNY_Top_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WNY_Top_TableViewCell"];
        cell.model = _model;
        cell.othersHomePage = ^(){
            OthersHomePageViewController *others = [[OthersHomePageViewController alloc] init];
            others.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:others animated:YES];
        
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 2){
        TunerDetailCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TunerDetailCommentTableViewCellID];
        if (!cell) {
            cell = [[TunerDetailCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TunerDetailCommentTableViewCellID];
            
        }
        [cell setLayout:_commentArray[indexPath.row]];
        
        [_cellHeightArray replaceObjectAtIndex:indexPath.row withObject:cell.height];
        
        cell.headImgClick = ^(){
            NSLog(@"跳到第%ld个用户主页", indexPath.row);
            OthersHomePageViewController *others = [[OthersHomePageViewController alloc] init];
            others.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:others animated:YES];
        };
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        EventRegistrationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:EventRegistrationTableViewCellID];
        __weak typeof(self) weakself = self;

        cell.click = ^(int temp){
            switch (temp) {
                case 0://报名
                {
                    
                    NSLog(@"报名");
//                    [self loadUI];
                    alerView  = [[InChargeAlertView alloc] initWithFrame:CGRectMake(20, 100, Screen_Width - 40, 50)];
                    [self.view addSubview:alerView];
                    
                    alerView.MakeSureBlock = ^(NSString *temp){
                        
                        if ([temp isEqualToString:@"大图"]) {
                            [weakself loadUI];
                            
                        }else {
                            ListScreenViewController *list = [[ListScreenViewController alloc] init];
                            [weakself.navigationController pushViewController:list animated:YES];
                        }
                    };
                }
                    break;
                case 1://评论
                    NSLog(@"评论");
                    break;
                case 2://分享
                    NSLog(@"分享");
                    break;
                default:
                    break;
            }
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
- (void)loadUI{
    // 创建视图
    self.backBtn.hidden = NO;
    self.container = [[CCDraggableContainer alloc] initWithFrame:CGRectMake(30, CCWidth/3 + 64, CCWidth - 60, CCWidth + 60) style:0];
    self.container.delegate = self;
    self.container.dataSource = self;
    [self.backBtn addSubview:self.container];
    [self.container reloadData];

    UIButton *a = [UIButton buttonWithType:0];
    [self.backBtn addSubview:a];
    [a setBackgroundImage:[UIImage imageNamed:@"nope.png"] forState:0];
//    a.sd_layout.leftSpaceToView(self.view,10).topSpaceToView(self.view,Screen_Width/2 + 64).widthIs(70).heightIs(70);
    a.frame = CGRectMake(10,Screen_Width/2 + 190, 70, 70);
    [a addTarget: self action:@selector(disLikeAction) forControlEvents:UIControlEventTouchUpInside];
    _disLike = a;
    
    // 喜欢
    UIButton *b = [UIButton buttonWithType:0];
    [self.backBtn addSubview:b];
    [b setBackgroundImage:[UIImage imageNamed:@"liked.png"] forState:0];
//    b.sd_layout.rightSpaceToView(self.view,10).topSpaceToView(self.view,Screen_Width/2 + 64).widthIs(70).heightIs(70);
    b.frame = CGRectMake(Screen_Width - 80,Screen_Width/2 + 190, 70, 70);
    [b addTarget: self action:@selector(LikeAction) forControlEvents:UIControlEventTouchUpInside];
    _likeBtn = b;
    
}
- (void)disLikeAction{
    [self.container removeFormDirection:CCDraggableDirectionLeft];
    
    
}
- (void)LikeAction{
    [self.container removeFormDirection:CCDraggableDirectionRight];
    
    
}
#pragma mark - CCDraggableContainer Delegate

- (void)draggableContainer:(CCDraggableContainer *)draggableContainer draggableDirection:(CCDraggableDirection)draggableDirection widthRatio:(CGFloat)widthRatio heightRatio:(CGFloat)heightRatio {
    
    CGFloat scale = 1 + ((kBoundaryRatio > fabs(widthRatio) ? fabs(widthRatio) : kBoundaryRatio)) / 4;
    if (draggableDirection == CCDraggableDirectionLeft) {
        //  不喜欢
        self.disLike.transform = CGAffineTransformMakeScale(scale, scale);
    }
    if (draggableDirection == CCDraggableDirectionRight) {
        // 喜欢 膨胀下
        self.likeBtn.transform = CGAffineTransformMakeScale(scale, scale);
    }
}

- (void)draggableContainer:(CCDraggableContainer *)draggableContainer cardView:(CCDraggableCardView *)cardView didSelectIndex:(NSInteger)didSelectIndex {
    
    NSLog(@"点击了Tag为%ld的Card", (long)didSelectIndex);
    
}
//  结束了 重来
- (void)draggableContainer:(CCDraggableContainer *)draggableContainer finishedDraggableLastCard:(BOOL)finishedDraggableLastCard {
    
    [draggableContainer reloadData];
}
- (CCDraggableCardView *)draggableContainer:(CCDraggableContainer *)draggableContainer viewForIndex:(NSInteger)index {
    
    CustomCardView *cardView = [[CustomCardView alloc] initWithFrame:draggableContainer.bounds];
    [cardView installData:[_dataSources objectAtIndex:index]];
    return cardView;
}

- (NSInteger)numberOfIndexs {
    return _dataSources.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else return 8;
}

- (CGFloat)cellContentViewWith
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    // 适配ios7横屏
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0, Screen_Width, Screen_Height)style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        [self.view addSubview:_tableView];
    }
    return _tableView;
}


@end